package kr.hkit.powerjava.chapter18;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;

public class PanelJTable extends JPanel implements ActionListener {
	private JPanel panel_Top;
	private JPanel panel_Bottom;
	private JScrollPane scrollPane;
	private JLabel lblSelected;
	private JButton btnAdd;
	private JTable table;

	/**
	 * Create the panel.
	 */
	public PanelJTable() {

		initialize();
		tableCellAlginAndWidth();
	}

	private void tableCellAlginAndWidth() {
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);//가운데 정렬
		
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);//우측정렬
		
		TableColumnModel cModel = table.getColumnModel();
		int columnCount = cModel.getColumnCount();//칼럼의 수
		
		cModel.getColumn(0).setPreferredWidth(70);
		cModel.getColumn(0).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(1).setPreferredWidth(70);
		cModel.getColumn(1).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(2).setPreferredWidth(70);
		cModel.getColumn(2).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(3).setPreferredWidth(70);
		cModel.getColumn(3).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(4).setPreferredWidth(150);//칼럼 폭
		cModel.getColumn(4).setCellRenderer(dtcrRight);
		
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));

		panel_Top = new JPanel();
		add(panel_Top, BorderLayout.NORTH);

		btnAdd = new JButton("추가");
		btnAdd.addActionListener(this);
		panel_Top.add(btnAdd);

		panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);

		final JLabel lblSelected = new JLabel("New label");
		panel_Bottom.add(lblSelected);

		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);

		String[] colNames = { "사원번호", "사원명", "직책", "매니저", "급여", "부서번호" };
		TableModel model = new DefaultTableModel(colNames, 0) {

			@Override
			public Class<?> getColumnClass(int columnIndex) {
				return getValueAt(0, columnIndex).getClass();
			}// 첫번째 행이 들어왔을 때 그 열의 데이터 타입을 결정함
		};
		table = new JTable(model);
		table.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				int row = table.getSelectedRow();
				/*int col = table.getSelectedColumn();
				try {
					String text = (String) table.getModel()
							.getValueAt(row, col);
					lblSelected.setText(text);
				} catch (ClassCastException e1) {
					int value = (int) table.getModel().getValueAt(row, col);
					lblSelected.setText(value+"");
				}*/
				StringBuilder sb = new StringBuilder();
				for(int i = 0; i<table.getColumnCount(); i++){
					sb.append(table.getModel().getValueAt(row, i) + " ");
				}
				lblSelected.setText(sb.toString());
			}

		});
		table.setAutoCreateRowSorter(true);
		scrollPane.setViewportView(table);
		
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd) {
			actionPerformedBtnAdd(e);
		}
	}

	protected void actionPerformedBtnAdd(ActionEvent e) {
		DecimalFormat df = new DecimalFormat("#, ###");
		DefaultTableModel model = (DefaultTableModel) table.getModel();

		Object[] newRow = { new Integer((int) (Math.random() * 100) + 1),
				"김태희", "사원", new Integer((int) (Math.random() * 100) + 1),
				df.format(new Integer((int) (Math.random() * 100000) + 1)),
				new Integer((int) (Math.random() * 100) + 1) };

		model.addRow(newRow);
	}
}
